home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 November: Tool Chest / Dev.CD Nov 96 TC / Dev.CD Nov 96 TC.toast / Sample Code / Text / SimpleText Sample / ThreeDMetafile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-12  |  11.6 KB  |  364 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ThreeDMetafile.c
  3.  
  4.     Contains:    3D viewer support for SimpleText
  5.  
  6.     Version:    SimpleText 1.4 or later
  7.  
  8. ** Copyright 1994-1996 Apple Computer. All rights reserved.
  9. **
  10. **    You may incorporate this sample code into your applications without
  11. **    restriction, though the sample code has been provided "AS IS" and the
  12. **    responsibility for its operation is 100% yours.  However, what you are
  13. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  14. **    after having made changes. If you're going to re-distribute the source,
  15. **    we require that you make it clear in the source that the code was
  16. **    descended from Apple Sample Code, but that you've made changes.
  17.  
  18. */
  19.  
  20.  
  21.  
  22. #include "MacIncludes.h"
  23. #include <String.h>
  24. #include "ThreeDMetafile.h"
  25.  
  26.  
  27. static GlobalEntry    gProcPointers[] =
  28.     {
  29.     {uppViewerLibNewProcInfo, nil},
  30.     {uppViewerLibDisposeProcInfo, nil},
  31.  
  32.     {uppViewerLibUseFileProcInfo, nil},
  33.     {uppViewerLibUseDataProcInfo, nil},
  34.     
  35.     {uppViewerLibWriteFileProcInfo, nil},
  36.     {uppViewerLibWriteDataProcInfo, nil},
  37.     
  38.     {uppViewerLibDrawProcInfo, nil},
  39.  
  40.     {uppViewerLibGetPictProcInfo, nil},
  41.     {uppViewerLibGetButtonRectProcInfo, nil},
  42.  
  43.     {uppViewerLibGetDimensionProcInfo, nil},
  44.  
  45.     {uppViewerLibSetFlagsProcInfo, nil},
  46.     {uppViewerLibGetFlagsProcInfo, nil},
  47.  
  48.     {uppViewerLibSetBoundsProcInfo, nil},
  49.     {uppViewerLibGetBoundsProcInfo, nil},
  50.  
  51.     {uppViewerLibSetPortProcInfo, nil},
  52.     {uppViewerLibGetPortProcInfo, nil},
  53.  
  54.     {uppViewerLibEventProcInfo, nil},
  55.     {uppViewerLibAdjustCursorProcInfo, nil},
  56.  
  57.     {uppViewerLibGetStateProcInfo, nil},
  58.  
  59.     {uppViewerLibClearProcInfo, nil},
  60.     {uppViewerLibCutProcInfo, nil},
  61.     {uppViewerLibCopyProcInfo, nil},
  62.     {uppViewerLibPasteProcInfo, nil}
  63.     };    
  64.     
  65.  
  66. // --------------------------------------------------------------------------------------------------------------
  67. // PRIVATE ROUTINES
  68. // --------------------------------------------------------------------------------------------------------------
  69. static UniversalProcPtr MakeARoutineDescriptor(void *theProc, ProcInfoType theProcInfo)
  70. /*
  71.     The Symantec compiler doesn't like jumping into staticly done routine descriptors,
  72.     so we have to actually allocate them as pointers.  Sigh.
  73. */
  74. {
  75.     RoutineDescriptor rd = BUILD_ROUTINE_DESCRIPTOR(0, nil);
  76.     
  77.     Ptr    newPtr;
  78.     
  79.     rd.routineRecords[0].procInfo = theProcInfo;
  80.     rd.routineRecords[0].procDescriptor = theProc;
  81.     rd.routineRecords[0].ISA = kPowerPCISA + kPowerPCRTA;
  82.     newPtr = NewPtrClear(sizeof(rd));
  83.     if (newPtr)
  84.         BlockMove(&rd, newPtr, sizeof(rd));
  85.     return((UniversalProcPtr) newPtr);
  86.     
  87. } // MakeARoutineDescriptor
  88.  
  89. // --------------------------------------------------------------------------------------------------------------
  90. static void FillInEntryPoints(void)
  91. {
  92.     long                result;
  93.     
  94.     if ( 
  95.         (Gestalt('qd3d', &result) == noErr) && 
  96.         (Gestalt('sysa', &result) == noErr) && (result == 2) 
  97.         )
  98.         {
  99.         CFragSymbolClass    symClass;
  100.         CFragConnectionID    connID;
  101.         Ptr                 mainAddr;
  102.         Str255                errName;
  103.         long                procID = 0;
  104.         
  105.         if (GetSharedLibrary("\pQD3DViewerLib", 'pwpc', 1, &connID, &mainAddr, errName) == noErr)
  106.             {
  107.             FindSymbol(connID, "\pQ3ViewerNew", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  108.             FindSymbol(connID, "\pQ3ViewerDispose", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  109.             
  110.             FindSymbol(connID, "\pQ3ViewerUseFile", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  111.             FindSymbol(connID, "\pQ3ViewerUseData", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  112.  
  113.             FindSymbol(connID, "\pQ3ViewerWriteFile", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  114.             FindSymbol(connID, "\pQ3ViewerWriteData", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  115.  
  116.             FindSymbol(connID, "\pQ3ViewerDraw", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  117.  
  118.             FindSymbol(connID, "\pQ3ViewerGetPict", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  119.             FindSymbol(connID, "\pQ3ViewerGetButtonRect", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  120.  
  121.             FindSymbol(connID, "\pQ3ViewerGetDimension", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  122.  
  123.             FindSymbol(connID, "\pQ3ViewerSetFlags", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  124.             FindSymbol(connID, "\pQ3ViewerGetFlags", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  125.  
  126.             FindSymbol(connID, "\pQ3ViewerSetBounds", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  127.             FindSymbol(connID, "\pQ3ViewerGetBounds", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  128.  
  129.             FindSymbol(connID, "\pQ3ViewerSetPort", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  130.             FindSymbol(connID, "\pQ3ViewerGetPort", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  131.  
  132.             FindSymbol(connID, "\pQ3ViewerEvent", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  133.             FindSymbol(connID, "\pQ3ViewerAdjustCursor", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  134.  
  135.             FindSymbol(connID, "\pQ3ViewerGetState", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  136.  
  137.             FindSymbol(connID, "\pQ3ViewerClear", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  138.             FindSymbol(connID, "\pQ3ViewerCut", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  139.             FindSymbol(connID, "\pQ3ViewerCopy", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  140.             FindSymbol(connID, "\pQ3ViewerPaste", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  141.             }
  142.         
  143.         // build the routine descriptors
  144.         {
  145.         short    i;
  146.         
  147.         for (i = 0; i < procID; ++i)
  148.             gProcPointers[i].entry = MakeARoutineDescriptor(gProcPointers[i].entry, gProcPointers[i].info);
  149.         }
  150.         
  151.         // as long as it isn't NIL, we're OK
  152.         if (gProcPointers[0].entry)
  153.             gMachineInfo.haveThreeD = true;
  154.         }
  155.  
  156. } // FillInEntryPoints
  157.  
  158. // --------------------------------------------------------------------------------------------------------------
  159. // OOP INTERFACE ROUTINES
  160. // --------------------------------------------------------------------------------------------------------------
  161.  
  162. static OSErr ThreeDCloseWindow(
  163.             WindowRef pWindow, 
  164.             WindowDataPtr pData)
  165. {
  166. #pragma unused (pWindow)
  167.  
  168.  
  169.     ViewerLib_Dispose( ((ThreeDDataPtr)pData)->viewerObject);
  170.  
  171.     return(noErr);
  172.     
  173. } // ThreeDCloseWindow
  174.  
  175. // --------------------------------------------------------------------------------------------------------------
  176. static OSErr ThreeDGetBalloon(
  177.             WindowRef pWindow, 
  178.             WindowDataPtr pData, 
  179.             Point *localMouse,
  180.             short * returnedBalloonIndex, 
  181.             Rect *returnedRectangle)
  182. {
  183. #pragma unused (pWindow, pData, localMouse, returnedBalloonIndex, returnedRectangle)
  184.  
  185.     *returnedBalloonIndex = iDidTheBalloon;
  186.     
  187.     return(noErr);
  188.     
  189. } // ThreeDGetBalloon
  190.  
  191. // --------------------------------------------------------------------------------------------------------------
  192. static Boolean    ThreeDFilterEvent(
  193.             WindowRef pWindow, 
  194.             WindowDataPtr pData, 
  195.             EventRecord *pEvent)
  196. {
  197. #pragma unused (pWindow)
  198.     
  199.     if (pEvent->what == activateEvt)
  200.         {
  201.         if (pEvent->modifiers & activeFlag)
  202.             ViewerLib_SetFlags(((ThreeDDataPtr)pData)->viewerObject, ViewerLib_GetFlags(((ThreeDDataPtr)pData)->viewerObject) | kQ3ViewerActive);
  203.         else
  204.             ViewerLib_SetFlags(((ThreeDDataPtr)pData)->viewerObject, ViewerLib_GetFlags(((ThreeDDataPtr)pData)->viewerObject) & ~kQ3ViewerActive);
  205.         }
  206.         
  207.     return((Boolean)ViewerLib_Event (((ThreeDDataPtr)pData)->viewerObject, pEvent));
  208.     
  209. } // ThreeDFilterEvent
  210.  
  211.  
  212. // --------------------------------------------------------------------------------------------------------------
  213. static OSErr ThreeDAdjustCursor(WindowRef pWindow, WindowDataPtr pData, Point *localMouse, Rect *globalRect)
  214. {
  215. #pragma unused (pWindow, globalRect)
  216.  
  217.     OSErr anErr = noErr;
  218.  
  219.     ViewerLib_AdjustCursor(((ThreeDDataPtr)pData)->viewerObject, localMouse);
  220.     
  221.     return(eActionAlreadyHandled);
  222.     
  223. } // ThreeDAdjustCursor
  224.  
  225. // --------------------------------------------------------------------------------------------------------------
  226. static OSErr ThreeDGetCoachRectangle(WindowRef pWindow, WindowDataPtr pData, Rect *pRect, Ptr name)
  227. {
  228. #pragma unused (pWindow)
  229.  
  230.     OSErr anErr = fnfErr;
  231.     
  232.     if (strcmp(name, "Camera") == 0)
  233.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonCamera, pRect);
  234.     if (strcmp(name, "Truck") == 0)
  235.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonTruck, pRect);
  236.     if (strcmp(name, "Orbit") == 0)
  237.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonOrbit, pRect);
  238.     if (strcmp(name, "Zoom") == 0)
  239.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonZoom, pRect);
  240.     if (strcmp(name, "Dolly") == 0)
  241.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonDolly, pRect);
  242.             
  243.     return(anErr);
  244.     
  245. } // ThreeDGetCoachRectangle
  246.  
  247. // --------------------------------------------------------------------------------------------------------------
  248. void ThreeDGetFileTypes(
  249.         OSType * pFileTypes,
  250.         OSType * pDocumentTypes,
  251.         short * numTypes)
  252. {
  253.     if (!gMachineInfo.haveThreeD)
  254.         FillInEntryPoints();
  255.         
  256.     if (gMachineInfo.haveThreeD)
  257.         {
  258.         pFileTypes[*numTypes]         = '3DMF';
  259.         pDocumentTypes[*numTypes]     = kThreeDWindow;
  260.         (*numTypes)++;
  261.         }
  262.     
  263. } // ThreeDGetFileTypes
  264.  
  265. // --------------------------------------------------------------------------------------------------------------
  266. static OSErr    ThreeDUpdateWindow(WindowRef pWindow, WindowDataPtr pData)
  267. {
  268. #pragma unused (pWindow)
  269.  
  270.     
  271.     ViewerLib_Draw(((ThreeDDataPtr)pData)->viewerObject);
  272.     
  273.     return(noErr);
  274.     
  275. } // ThreeDUpdateWindow
  276.  
  277. // --------------------------------------------------------------------------------------------------------------
  278. static OSErr    ThreeDAdjustMenus(WindowRef pWindow, WindowDataPtr pData)
  279. {
  280. #pragma unused (pWindow)
  281.  
  282.     OSErr anErr = noErr;
  283.     
  284.     if (ViewerLib_GetState(((ThreeDDataPtr)pData)->viewerObject) ) 
  285.         EnableCommand(cCopy);
  286.  
  287.     return(anErr);
  288.     
  289. } // ThreeDAdjustMenus
  290.  
  291. // --------------------------------------------------------------------------------------------------------------
  292. static OSErr    ThreeDCommand(WindowRef pWindow, WindowDataPtr pData, short commandID, long menuResult)
  293. {
  294. #pragma unused (menuResult)
  295.  
  296.     OSErr                        anErr = noErr;
  297.     ViewerObject                 theViewer = ((ThreeDDataPtr)pData)->viewerObject;
  298.     
  299.     SetPort((GrafPtr) GetWindowPort(pWindow));
  300.         
  301.     switch (commandID) 
  302.         {
  303.         case cCopy:
  304.             anErr = ViewerLib_Copy(theViewer);
  305.             if (anErr == noErr)
  306.                 anErr = eActionAlreadyHandled;
  307.             break;
  308.         } // switch (commandID)
  309.         
  310.     return(anErr);
  311.     
  312. } // ThreeDCommand
  313.  
  314. // --------------------------------------------------------------------------------------------------------------
  315. static OSErr    ThreeDMakeWindow(
  316.             WindowRef pWindow,
  317.             WindowDataPtr pData)
  318. {
  319.     OSErr                        anErr = noErr;
  320.     ViewerObject                viewerObj;
  321.  
  322.     pData->pCloseWindow         = (CloseWindowProc)            ThreeDCloseWindow;
  323.     pData->pFilterEvent         = (FilterEventProc)            ThreeDFilterEvent;
  324.     pData->pGetBalloon             = (GetBalloonProc)            ThreeDGetBalloon;
  325.     pData->pUpdateWindow         = (UpdateWindowProc)        ThreeDUpdateWindow;
  326.     pData->pAdjustMenus         = (AdjustMenusProc)            ThreeDAdjustMenus;
  327.     pData->pCommand                 = (CommandProc)                ThreeDCommand;
  328.     pData->pAdjustCursor         = (AdjustCursorProc)        ThreeDAdjustCursor;
  329.     pData->pGetCoachRectangle    = (GetCoachRectangleProc)    ThreeDGetCoachRectangle;
  330.  
  331.     pData->dragWindowAligned    = 0;
  332.  
  333.     viewerObj = ViewerLib_New((CGrafPtr )pWindow,  &pData->contentRect,  kQ3ViewerDefault | kQ3ViewerDraggingOff); 
  334.  
  335.     if (viewerObj == 0)
  336.         {
  337.         anErr = memFullErr;
  338.         }
  339.     else
  340.         {
  341.         anErr = ViewerLib_UseFile(viewerObj, pData->dataRefNum);
  342.         if (anErr == noErr)
  343.             ((ThreeDDataPtr)pData)->viewerObject = viewerObj;
  344.         else
  345.             ViewerLib_Dispose(viewerObj);
  346.         
  347.         }
  348.     
  349.     return(anErr);
  350.     
  351. } // ThreeDMakeWindow
  352.  
  353. // --------------------------------------------------------------------------------------------------------------
  354. OSErr    ThreeDPreflightWindow(PreflightPtr pPreflightData)
  355. {    
  356.     pPreflightData->continueWithOpen     = true;
  357.     pPreflightData->makeProcPtr         = ThreeDMakeWindow;
  358.     pPreflightData->resourceID            = kThreeDWindowID;
  359.     pPreflightData->storageSize         = sizeof(ThreeDDataRecord);
  360.  
  361.     return(noErr);
  362.     
  363. } // ThreeDPreflightWindow
  364.